<template>
  <div
    :class="{
      'theme-default': type === 'default',
      'theme-orange': type === 'orange'
    }"
  >
    <Input
      v-bind="$attrs"
      v-model="keywords"
      v-on="$listeners"
      :style="{ width: width || '250px' }"
    >
      <template slot="prepend">
        <Select v-model="titleType" placeholder="请选择">
          <Option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </Select>
      </template>
      <div slot="append" @click="handleClick">
        搜索
      </div>
    </Input>
  </div>
</template>

<script>
import { Input, Select, Option } from "element-ui"

export default {
  name: "MSearchbar",
  components: { Input, Select, Option },
  props: {
    type: {
      type: String,
      default: "default"
    },
    width: String
  },
  data() {
    return {
      options: [
        {
          value: "1",
          label: "找包装"
        },
        {
          value: "2",
          label: "找服务"
        },
        {
          value: "3",
          label: "找企业"
        }
      ],
      titleType: "1",
      keywords: ""
    }
  },
  methods: {
    change(val) {
      console.log(val)
    },
    handleClick() {
      if (!this.keywords) {
        return this.$toastVant("请输入关键词！")
      }
      const title = this.titleType
      if (title === "1") {
        this.$router.push({
          name: "SearchPackage",
          params: {
            keyword: this.keywords
          },
          query: {
            type: 1,
            t: Date.now()
          }
        })
      } else if (title === "2") {
        this.$router.push({
          name: "SearchPackage",
          params: {
            keyword: this.keywords
          },
          query: {
            type: 0,
            t: Date.now()
          }
        })
      } else if (title === "3") {
        this.$router.push({
          name: "SearchCompany",
          params: {
            keyword: this.keywords
          },
          query: {
            t: Date.now()
          }
        })
      }
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/style/common.scss";

.icon {
  position: relative;
  top: 2px;
}
/deep/ .el-select .el-input .el-select__caret {
  margin-right: 15px;
}

.theme-default {
  /deep/ .el-input__inner:focus {
    border-color: #dcdfe6;
  }

  /deep/ .el-input-group__prepend {
    padding: 0 10px;
    border-top-left-radius: 45px;
    border-bottom-left-radius: 45px;
    background-color: #fff;
    border-color: #e38b2e;
  }

  /deep/ .el-input-group__append {
    padding: 0 20px;
    border-top-right-radius: 45px;
    border-bottom-right-radius: 45px;
    background-color: $theme-color-1;
    color: #fff;
    cursor: pointer;
    border: none #e38b2e;
  }
  /deep/ .el-select {
    width: 100px;
    height: 30px;
    text-align: center;
    margin-left: 10px;
    margin-right: 5px;
  }

  /deep/ .el-input__inner {
    border-left: none;
    border-right: none;
    border-color: #e38b2e !important;
    // border: none;
    padding: 0 5px;
    background: #fff;
  }
}

.theme-orange {
  /deep/ .el-input__inner:focus {
    border-color: #dcdfe6;
  }

  /deep/ .el-input-group__prepend {
    padding: 0 5px 0 10px;
    border-top-left-radius: 45px;
    border-bottom-left-radius: 45px;
    background-color: $theme-color-1;
    border-color: #e38b2e;
  }

  /deep/ .el-input-group__append {
    padding: 0 20px;
    border-top-right-radius: 45px;
    border-bottom-right-radius: 45px;
    background-color: #fff;
    color: $theme-color-1;
    border: none;
    cursor: pointer;
  }

  /deep/ .el-input__inner {
    border-left: none;
    border-right: none;
    // border: none;
    border-color: #e38b2e !important;
    background-color: $theme-color-1;
    color: #fff;
    padding: 0 5px;

    &::placeholder {
      color: #fff;
    }
  }
}
</style>
